# Import PFX Certificate to Windows Certificate Store
param(
    [Parameter(Mandatory=$true)]
    [string]$PfxPath,

    [Parameter(Mandatory=$true)]
    [string]$Password,

    [Parameter(Mandatory=$false)]
    [string]$StoreLocation = "LocalMachine",

    [Parameter(Mandatory=$false)]
    [string]$StoreName = "My"
)

try {
    # 检查文件是否存在
    if (-not (Test-Path $PfxPath)) {
        Write-Error "PFX file not found: $PfxPath"
        exit 1
    }

    # 转换密码为 SecureString
    $securePassword = ConvertTo-SecureString -String $Password -Force -AsPlainText

    # 导入证书
    $cert = Import-PfxCertificate `
        -FilePath $PfxPath `
        -CertStoreLocation "Cert:\$StoreLocation\$StoreName" `
        -Password $securePassword `
        -Exportable

    # 输出证书指纹
    Write-Output $cert.Thumbprint
    exit 0

} catch {
    Write-Error $_.Exception.Message
    exit 1
}
